package com.telecom.util;

import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.SQLException;

public class CustomSqlSession implements SqlSession{
    private DbProfile profile;
    private Connection connection;
    public CustomSqlSession(DbProfile profile) {
        this.profile=profile;
        connection=DBUtil.getConnection(profile);
    }

    @Override
    public <T> T getMapper(Class<T> type) {
        return (T)Proxy.newProxyInstance(type.getClassLoader(), new Class[]{type}, new ProxyImpl(profile.getMappers(),
                connection));

    }

    @Override
    public void close() {
        if (connection!=null)
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
    }
}
